package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.service.rock.BillingOwnerService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @Author wuguangxi
 * @Description
 * @Date 2024/5/7 18:59
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/billingOwner")
public class BillingOwnerController {
    @Autowired
    private BillingOwnerService billingOwnerService;

    @RestGrpcMapping("/findFlinkList.do")
    public NTSTATUS findFlinkList(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            BillingOwnerController.Params aa = params.driverObject(BillingOwnerController.Params.class);
            result.setData(billingOwnerService.findFlinkList(handleDT(aa.getDt()), aa.getOa()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/findOtherList.do")
    public NTSTATUS findOtherList(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            BillingOwnerController.Params aa = params.driverObject(BillingOwnerController.Params.class);
            result.setData(billingOwnerService.findOtherList(handleDT(aa.getDt()), aa.getOa(), aa.getComponent()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @Data
    public static class Params {
        private String dt;

        private String oa;

        private String component;
    }

    private String handleDT(String dt){
        try {
            return dt.substring(0,4) + "-" + dt.substring(4,6) + "-" + dt.substring(6);
        }catch (Exception e){
            return "";
        }
    }

}
